import React, { Component } from 'react'
import axios from 'axios'
export default class Son1 extends Component {
    render() {
        return (
            <div>我是son1组件</div>
        )
    }
    state = {
        timer: null,
        controller: new AbortController()
    }
    componentDidMount() {
        console.log('componentDidMount-son1');
        let timer1 = setInterval(() => {
            console.log('定时器');
        }, 1000)
        // 设置定时器
        this.setState({
            timer: timer1
        })
        // 页面绑定事件
        document.onclick = () => {
            console.log('document点击事件');
        }
        // 网络请求
        axios.get('https://reactapi.iynn.cn/captcha/api/math', {
            signal: this.state.controller.signal
        }).then(res1 => {
            console.log('res1', res1);
        }).catch(err => {
            console.log(err);
        })
    };
    componentWillUnmount() {
        console.log('componentWillUnmount-son1');
        // 该生命周期就是用来清理副作用的
        clearInterval(this.state.timer)
        // 清除dom0 级事件
        document.onclick = null;
        // 取消网络请求
        this.state.controller.abort()
    }
}
